using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using MageDefender.Interfaces;
using MageDefender.ParticleEngine;

namespace MageDefender.Scenes
{
    public class CreditsScene : Microsoft.Xna.Framework.DrawableGameComponent
    {
        ParticleSystem magicMissilePartilces;
        ParticleSystem magicMissilePartilces2;
        ParticleSystem magicMissilePartilces3;
        
        GameClasses.Camera m_Camera;


        Model m_Model;
        Matrix renderMatrix, worldMatrix;
        Matrix[] bones;


        bool startGame;

        public bool StartGame
        {
            get { return startGame; }
            set { startGame = value; }
        }

        Texture2D texWorld;
        Texture2D texTest;
        Texture2D texMage;
        SpriteBatch spriteBatch;

        float spinWorld = 0.0f;

        public CreditsScene(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            startGame = false;
            m_Model = null;

            m_Camera = new GameClasses.Camera(game);
            magicMissilePartilces = new PressStartParticleSystem(game, game.Content);
            magicMissilePartilces2 = new PressStart2ParticleSystem(game, game.Content);
            magicMissilePartilces3 = new PressStart3ParticleSystem(game, game.Content);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            texTest = this.Game.Content.Load<Texture2D>("Textures\\Credits");
            texMage = this.Game.Content.Load<Texture2D>("Textures\\Start_Mage");
            texWorld = this.Game.Content.Load<Texture2D>("Models\\Start_loop");

            m_Camera.Initialize();
            m_Camera.Position = new Vector3(0, 0, 3000.0f);
            m_Camera.LookAt = new Vector3(0, 0, 0);

            magicMissilePartilces.Initialize();
            magicMissilePartilces2.Initialize();
            magicMissilePartilces3.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            m_Model = Game.Content.Load<Model>("Models\\world_spin");
            bones = new Matrix[this.m_Model.Bones.Count];
            this.m_Model.CopyAbsoluteBoneTransformsTo(bones);

            base.LoadContent();
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (spinWorld <= 4500)
            {
                spinWorld += ((float)gameTime.ElapsedGameTime.Milliseconds / 50.0f);
            }

            IPlayer player = (IPlayer)Game.Services.GetService(typeof(IPlayer));

            Matrix objectMatrix = Matrix.CreateRotationX(0) * Matrix.CreateRotationY(spinWorld) * Matrix.CreateTranslation(0.0f, -21.5f, 0.0f);
            Matrix renderMatrix = Matrix.CreateScale(0.93f);
            worldMatrix = objectMatrix * renderMatrix;

            GamePadState playerState = GamePad.GetState(player.PlayerIndexSaved);
            KeyboardState keyboard = Keyboard.GetState();

            if (GamePad.GetState(player.PlayerIndexSaved).Buttons.Start == ButtonState.Pressed || keyboard.IsKeyDown(Keys.E))
            {
                startGame = true;
            }


            m_Camera.Update(gameTime);
            magicMissilePartilces.SetCamera(m_Camera.ViewMatrix, m_Camera.ProjectionMatrix);
            magicMissilePartilces2.SetCamera(m_Camera.ViewMatrix, m_Camera.ProjectionMatrix);
            magicMissilePartilces3.SetCamera(m_Camera.ViewMatrix, m_Camera.ProjectionMatrix);

            for (int i = 0; i < 1; i++)
            {
                magicMissilePartilces.AddParticle(new Vector3(21.0f,-2.0f,0.0f), Vector3.Zero);
            }
            for (int i = 0; i < 5; i++)
            {
                magicMissilePartilces2.AddParticle(new Vector3(21.0f + (float)Math.Sin((float)gameTime.TotalGameTime.Milliseconds), -2.0f + (float)Math.Cos((float)gameTime.TotalGameTime.Milliseconds), 0.0f), Vector3.Zero);
            }
            for (int i = 0; i < 5; i++)
            {
                magicMissilePartilces3.AddParticle(new Vector3(21.0f + (float)Math.Sin((float)gameTime.TotalGameTime.Milliseconds), -2.0f + (float)Math.Cos((float)gameTime.TotalGameTime.Milliseconds), 0.0f), Vector3.Zero);
            }

            magicMissilePartilces.Update(gameTime);
            magicMissilePartilces2.Update(gameTime);
            magicMissilePartilces3.Update(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(new Color(21,21,21,255));

            //foreach (ModelMesh mesh in m_Model.Meshes)
            //{
            //    // This is where the mesh orientation is set, as well as our camera and projection.  
            //    foreach (BasicEffect effect2 in mesh.Effects)
            //    {
            //        effect2.EnableDefaultLighting();
            //        effect2.PreferPerPixelLighting = true;

            //        //effect2.AmbientLightColor = AmbientLightColor;
            //        //effect2.DiffuseColor = DiffuseLightColor;
            //        //effect2.SpecularColor = SpecularLightColor;
            //        //effect2.SpecularPower = SpecularPower;
            //        effect2.Texture = texWorld;
            //        effect2.World = bones[mesh.ParentBone.Index] * worldMatrix;
            //        effect2.View = m_Camera.ViewMatrix;
            //        effect2.Projection = m_Camera.ProjectionMatrix;
            //    }
            //    mesh.Draw(SaveStateMode.SaveState);
            //}

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            spriteBatch.Draw(texWorld, new Rectangle((int)-spinWorld, 0, 5824, Game.GraphicsDevice.PresentationParameters.BackBufferHeight), Color.DarkGray);
            spriteBatch.Draw(texMage, new Rectangle(0, 0, Game.GraphicsDevice.PresentationParameters.BackBufferWidth, Game.GraphicsDevice.PresentationParameters.BackBufferHeight), Color.White);
            spriteBatch.Draw(texTest, new Rectangle(0, 0, Game.GraphicsDevice.PresentationParameters.BackBufferWidth, Game.GraphicsDevice.PresentationParameters.BackBufferHeight), Color.White);
            spriteBatch.End();

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            magicMissilePartilces.Draw(gameTime);
            magicMissilePartilces3.Draw(gameTime);
            magicMissilePartilces2.Draw(gameTime);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
